<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2022/2/24 10:18
 * Desc:票务价格管理 三方的票数过多 无报价后可能未及时下架等情况需要自动处理
 */
class Controller_Provider_Ticket_Product extends Stourweb_Controller
{
    public function before()
    {
        parent::before();
    }

    /**
     * 只更新三方票务的产品状态
     */
    public function action_auto_status()
    {
        $tickets = DB::select('id', 'spotid')
            ->from('spot_ticket')
            ->where('status', '=', 3)
            ->and_where(DB::expr(' AND interface_code is not NULL'))
            ->limit(10)
            ->order_by('modtime', 'ASC')
            ->execute()
            ->as_array();
        $check_time = strtotime(date("Y-m-d"));
        $result = array('status'=> true, 'total'=> 0, 'change'=> 0);
        if (!empty($tickets)) {
            $change = 0;
            $change_spot = array();
            foreach ($tickets as $ticket) {
                $ticketId = $ticket['id'];
                $price = DB::select()
                    ->from('spot_ticket_price')
                    ->where('ticketid', '=', $ticketId)
                    ->and_where('day', '>=', $check_time)
                    ->execute()
                    ->current();
                $update_data = array(
                    'modtime'=> time()
                );
                if (!$price) {
                    $update_data['status'] = 2;
                    array_push($change_spot, $ticket['spotid']);
                    $change++;
                }
                DB::update('spot_ticket')
                    ->set($update_data)
                    ->where('id', '=', $ticketId)
                    ->execute();
            }
            $result['change_spot'] = array_unique($change_spot);
            $result['change'] = $change;
            $result['total'] = count($tickets);
        }
        exit(json_encode($result));
    }
}